package 树;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Leedcode102二叉树的层序遍历 {
    List<List<Integer>> list = new LinkedList<>();
    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root == null){
            return list;
        }
        Queue queue = new LinkedList();
        queue.offer(root);

        while (!queue.isEmpty()){
            List<Integer> list1 = new LinkedList<>();
            int size = queue.size();
            for(int i = 0;i < size;i++){
                TreeNode node = (TreeNode) queue.poll();
                list1.add((Integer)node.val);
                if (node.left != null){
                    queue.offer(node.left);
                }
                if (node.right != null){
                    queue.offer(node.right);
                }
            }
            list.add(list1);
        }
        return list;
    }
}
